package com.web.exiaodao.remote.service.impl;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.entity.EBook;
import com.web.exiaodao.business.repository.EBookRepository;
import com.web.exiaodao.remote.service.ApiEBookService;
import com.web.exiaodao.remote.vo.EBookVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author: zhangl
 * @times: 2015-5-15下午06:46:35
 * 类的说明：前端APIEBook接口实现类
 **/
@Service("ApiEBookService")
@Transactional(readOnly = false)
public class ApiEBookServiceImpl extends BaseService<EBook> implements ApiEBookService {

    @Autowired
    EBookRepository eBookRepository;

    @Override
    public XaResult<EBookVo> findOne(Long tId) throws BusinessException {
        EBook obj = eBookRepository.findByIdAndStatusNot(tId, XaConstant.Status.delete);
        XaResult<EBookVo> xr = new XaResult<EBookVo>();
        if (XaUtil.isNotEmpty(obj)) {
            xr.setObject(JSON.parseObject(JSON.toJSONString(obj), EBookVo.class));
            xr.getObject().setId(obj.getId());
        } else {
            throw new BusinessException(XaConstant.Message.object_not_find);
        }
        return xr;
    }

    @Override
    public XaResult<List<EBookVo>> findListNEStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
        if (status == null) {// 默认显示非删除的所有数据
            status = XaConstant.Status.delete;
        }
        filters.put("status", new SearchFilter("status", Operator.NE, status));
        Page<EBook> page = eBookRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), EBook.class), pageable);
        XaResult<List<EBookVo>> xr = new XaResult<List<EBookVo>>();
        xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), EBookVo.class));
        for (int i = 0; i < page.getContent().size(); i++) {
            xr.getObject().get(i).setId(page.getContent().get(i).getId());
        }
        return xr;
    }

    @Override
    public XaResult<List<EBookVo>> findListEQStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
        if (status == null) {// 默认显示正常数据
            status = XaConstant.Status.valid;
        }
        filters.put("status", new SearchFilter("status", Operator.EQ, status));
        Page<EBook> page = eBookRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), EBook.class), pageable);
        XaResult<List<EBookVo>> xr = new XaResult<List<EBookVo>>();
        xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), EBookVo.class));
        for (int i = 0; i < page.getContent().size(); i++) {
            xr.getObject().get(i).setId(page.getContent().get(i).getId());
        }
        return xr;
    }

    @Override
    public XaResult<EBookVo> multiOperate(String modelIds,
                                          Integer status) throws BusinessException {
        XaResult<EBookVo> xr = new XaResult<EBookVo>();
        if (status == null) {
            status = XaConstant.Status.delete;
        }
        if (modelIds != null) {
            String[] ids = modelIds.split(",");
            for (String id : ids) {
                EBook obj = eBookRepository.findByIdAndStatusNot(Long.parseLong(id), status);
                if (XaUtil.isNotEmpty(obj)) {
                    obj.setStatus(status);
                    obj = eBookRepository.save(obj);
                    xr.setObject(JSON.parseObject(JSON.toJSONString(obj), EBookVo.class));
                    xr.getObject().setId(obj.getId());
                } else {
                    throw new BusinessException(XaConstant.Message.object_not_find);
                }
            }
        }
        return xr;
    }

    @Override
    public XaResult<EBookVo> createModel(EBook model)
            throws BusinessException {
        XaResult<EBookVo> xr = new XaResult<EBookVo>();
        EBook obj = eBookRepository.save(model);
        xr.setObject(JSON.parseObject(JSON.toJSONString(obj), EBookVo.class));
        xr.getObject().setId(obj.getId());
        return xr;
    }

    @Override
    public XaResult<EBookVo> updateModel(EBook model)
            throws BusinessException {
        EBook obj = eBookRepository.findOne(model.getId());
        XaResult<EBookVo> xr = new XaResult<EBookVo>();
        if (XaUtil.isNotEmpty(obj)) {
            obj.setTitle(model.getTitle());
            obj.setSource(model.getSource());
            obj.setEBookUrl(model.getEBookUrl());
            obj.setCover(model.getCover());
            obj = eBookRepository.save(obj);
            xr.setObject(JSON.parseObject(JSON.toJSONString(obj), EBookVo.class));
            xr.getObject().setId(obj.getId());
        } else {
            throw new BusinessException(XaConstant.Message.object_not_find);
        }
        return xr;
    }

}
